﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;


namespace sales.Controls
{
    //Incomplete
    public class gridOtherServiceDetail : common.controls.baseDataGridView 
    {
        public gridOtherServiceDetail()
        {
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
            this.AutoArrange();
            this.myOnFind += new OnCellEvent(FindData);
        }

        public enum ColumnNames
        {
            itemCode, itemSubCode, itemName, itemUnit, qty, price, fPrice,
            taxPercent, taxAmount, taxFAmount, amount, fAmount, notes
        };

        public void UpdateItemList()
        {
            UpdateItemList(null);
        }
        public void UpdateItemList(string itemCode)
        {
            if (itemCode == null)
            {
                for (int idx = 0; idx < this.Rows.Count; idx++)
                {
                    if (this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value == null) continue;
                    DataAccess.DataCache.FindITEM(this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value.ToString());
                }
            }
            else DataAccess.DataCache.FindITEM(itemCode);
        }
        public override void Init(BindingSource dataSource)
        {
            base.Init(dataSource);
            data.otherDS.OtherServiceDetailsDataTable detailTbl = (data.otherDS.OtherServiceDetailsDataTable)dataSource.DataSource;

            DataGridViewCellStyle amountCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
            DataGridViewCellStyle fAmountCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
            DataGridViewCellStyle qtyCellStyle = new System.Windows.Forms.DataGridViewCellStyle();

            fAmountCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            fAmountCellStyle.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskForeignAmt).ToString();
            fAmountCellStyle.NullValue = null;

            amountCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            amountCellStyle.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskLocalAmt).ToString();
            amountCellStyle.NullValue = null;

            qtyCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            qtyCellStyle.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskQtySales).ToString();
            qtyCellStyle.NullValue = null;

            // 
            // itemCode
            // 
            DataGridViewTextBoxColumn itemCode = new System.Windows.Forms.DataGridViewTextBoxColumn();
            itemCode.DataPropertyName =detailTbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã.hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 100;
            itemCode.Visible = true;


            // 
            // subCode
            // 
            DataAccess.DataCache.LoadITEMSUBCODE();
            BindingSource itemSubCodeSource = new BindingSource();
            itemSubCodeSource.DataSource = DataAccess.DataCache.myMasterDS.itemSubCode;

            DataGridViewComboBoxColumn itemSubCode = new DataGridViewComboBoxColumn();
            itemSubCode.DataPropertyName =detailTbl.itemSubCodeColumn.ColumnName;
            itemSubCode.HeaderText = " +";
            itemSubCode.Name = ColumnNames.itemSubCode.ToString();
            itemSubCode.Width = 50;
            itemSubCode.DataSource = itemSubCodeSource;
            itemSubCode.DisplayMember = DataAccess.DataCache.myMasterDS.itemSubCode.codeColumn.ColumnName;
            itemSubCode.ValueMember = DataAccess.DataCache.myMasterDS.itemSubCode.codeColumn.ColumnName;

            // 
            // itemName
            // 
            BindingSource itemSource = new BindingSource();
            itemSource.DataSource = DataAccess.DataCache.myMasterDS.Item;

            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataPropertyName =detailTbl.itemCodeColumn.ColumnName;
            itemName.DataSource = itemSource;
            itemName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemName.HeaderText = "     Diễn giải";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;
            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.Width = 200;
            itemName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            // 
            // itemUnit
            // 
            DataGridViewComboBoxColumn itemUnit = new DataGridViewComboBoxColumn();
            itemUnit.DataPropertyName =detailTbl.itemCodeColumn.ColumnName;
            itemUnit.DataSource = itemSource;
            itemUnit.DisplayMember = DataAccess.DataCache.myMasterDS.Item.unitColumn.ColumnName;
            itemUnit.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemUnit.HeaderText = "ĐVT";
            itemUnit.Name = ColumnNames.itemUnit.ToString();
            itemUnit.ReadOnly = true;
            itemUnit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemUnit.Width = 50;
            itemUnit.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemUnit.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            // 
            // Qty
            // 
            DataGridViewTextBoxColumn qty = new System.Windows.Forms.DataGridViewTextBoxColumn();
            qty.DataPropertyName =detailTbl.qtyColumn.ColumnName;
            qty.DefaultCellStyle = qtyCellStyle;
            qty.HeaderText = "Lượng";
            qty.Name = ColumnNames.qty.ToString();
            qty.Width = 50;

            // 
            // fPrice
            // 
            DataGridViewTextBoxColumn fPrice = new System.Windows.Forms.DataGridViewTextBoxColumn();
            fPrice.DataPropertyName =detailTbl.fPriceColumn.ColumnName;
            fPrice.DefaultCellStyle = fAmountCellStyle;
            fPrice.HeaderText = "Giá";
            fPrice.Name = ColumnNames.fPrice.ToString();
            fPrice.Width = 80;
            fPrice.Visible = false;

            // 
            // Price
            // 
            DataGridViewTextBoxColumn price = new System.Windows.Forms.DataGridViewTextBoxColumn();
            price.DataPropertyName =detailTbl.priceColumn.ColumnName;
            price.DefaultCellStyle = amountCellStyle;
            price.HeaderText = "Giá";
            price.Name = ColumnNames.price.ToString();
            price.Width = 80;
            price.Visible = false;

            // 
            // fAmount
            // 
            DataGridViewTextBoxColumn fAmount = new System.Windows.Forms.DataGridViewTextBoxColumn();
            fAmount.DataPropertyName =detailTbl.fAmountColumn.ColumnName;
            fAmount.DefaultCellStyle = fAmountCellStyle;
            fAmount.HeaderText = "Thành tiền";
            fAmount.Name = ColumnNames.fAmount.ToString();
            fAmount.Width = 90;
            fAmount.Visible = false;

            // 
            // Amount
            // 
            DataGridViewTextBoxColumn amount = new System.Windows.Forms.DataGridViewTextBoxColumn();
            amount.DataPropertyName =detailTbl.amountColumn.ColumnName;
            amount.DefaultCellStyle = amountCellStyle;
            amount.HeaderText = "Thành tiền";
            amount.Name = ColumnNames.amount.ToString();
            amount.Width = 90;
            amount.Visible = false;

            // 
            // taxPercent
            // 
            DataGridViewTextBoxColumn taxPercent = new System.Windows.Forms.DataGridViewTextBoxColumn();
            taxPercent.DataPropertyName =detailTbl.taxPercColumn.ColumnName;
            taxPercent.DefaultCellStyle = amountCellStyle;
            taxPercent.HeaderText = "%";
            taxPercent.Name = ColumnNames.taxPercent.ToString();
            taxPercent.Width = 30;
            taxPercent.Visible = false;

            // 
            // taxFAmount
            // 
            DataGridViewTextBoxColumn taxFAmount = new System.Windows.Forms.DataGridViewTextBoxColumn();
            taxFAmount.DataPropertyName =detailTbl.taxFAmtColumn.ColumnName;
            taxFAmount.DefaultCellStyle = fAmountCellStyle;
            taxFAmount.HeaderText = "Thuế";
            taxFAmount.Name = ColumnNames.taxFAmount.ToString();
            taxFAmount.Width = 80;
            taxFAmount.Visible = false;

            // taxAmount
            // 
            DataGridViewTextBoxColumn taxAmount = new System.Windows.Forms.DataGridViewTextBoxColumn();
            taxAmount.DataPropertyName =detailTbl.taxAmtColumn.ColumnName;
            taxAmount.DefaultCellStyle = amountCellStyle;
            taxAmount.HeaderText = "    Thuế";
            taxAmount.Name = ColumnNames.taxAmount.ToString();
            taxAmount.Width = 80;
            taxAmount.Visible = false;
            // 
            // Notes
            // 
            DataGridViewTextBoxColumn notes = new System.Windows.Forms.DataGridViewTextBoxColumn();
            notes.DataPropertyName =detailTbl.notesColumn.ColumnName;
            notes.HeaderText = "Ghi chú";
            notes.Name = ColumnNames.notes.ToString(); notes.Width = 250;
            notes.Visible = true;

            fAmount.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorHiLightBG1;
            fAmount.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorHiLightFG1;

            fPrice.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorHiLightBG1;
            fPrice.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorHiLightFG1;

            taxFAmount.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorHiLightBG1;
            taxFAmount.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorHiLightFG1;

            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                                    itemCode,itemSubCode, itemName,itemUnit,qty,fPrice ,price,fAmount,amount,taxPercent,taxFAmount,taxAmount,notes});
            this.DisbaleReadOnlyColumns();
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
        }
        public override void Refresh()
        {
            if (IsOnProcessing) return;
            try
            {
                IsOnProcessing = true;
                UpdateItemList(null);
                IsOnProcessing = false;
                base.Refresh();
            }
            catch (Exception er)
            {
                IsOnProcessing = false;
                ErrorHandler(this, er);
            }
        }
        
        private void FindData(object sender, common.controls.GridEventArgs arg)
        {
            string findCode = this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].EditedFormattedValue.ToString(); ;
            if (this.CurrentCell == null) return;
            //Code
            if (this.Columns[arg.ColumnIndex].Name.Trim() == ColumnNames.itemCode.ToString())
            {
                application.dataFind.myItemFind.Find(findCode, true);
                data.masterDS.ItemRow itemRow = application.dataFind.myItemFind.selectedDataRow;
                if (itemRow != null)
                {
                    this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = itemRow.itemCode;
                    UpdateItemList(itemRow.itemCode);
                }
                else this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = DBNull.Value;
                this.myDataSource.ResetCurrentItem();
                this.myDataSource.EndEdit(); 
            }
        }
    }
}
